package cn.fintecher.pangolin.service.business.model.request;

import cn.fintecher.pangolin.common.enums.CaseType;
import cn.fintecher.pangolin.common.enums.PayStatus;
import cn.fintecher.pangolin.common.web.SearchRequest;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.*;


@Data
public class ReturnCaseQueryRequest {

    @ApiModelProperty("委托方id")
    private String principalId;

    @ApiModelProperty("批次号")
    private String batchNumber;

    @ApiModelProperty("客户姓名")
    private String personalName;

    @ApiModelProperty("身份证号码")
    private String certificateNo;

    @ApiModelProperty("手机号")
    private String phone;

    @ApiModelProperty("卡号")
    private String cardNo;

    @ApiModelProperty("委案阶段")
    private String stage;

    @ApiModelProperty("地址")
    private String address;
    @ApiModelProperty(notes = "案件编号")
    private String caseNumber;

    @ApiModelProperty("单位名称")
    private String employerName;

    @ApiModelProperty("还款状态")
    private PayStatus payStatus;

    @ApiModelProperty("委案日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date delegationDateStart;

    @ApiModelProperty("委案日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date delegationDateEnd;

    @ApiModelProperty("逾期期数开始")
    private Integer overduePeriodsStart;

    @ApiModelProperty("逾期期数结束")
    private Integer overduePeriodsEnd;

    @ApiModelProperty("逾期天数开始")
    private Integer overdueDaysStart;

    @ApiModelProperty("逾期天数结束")
    private Integer overdueDaysEnd;

    @ApiModelProperty("城市")
    private String city;

    @ApiModelProperty("退案日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endCaseDateStart;

    @ApiModelProperty("退案日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endCaseDateEnd;

    @ApiModelProperty("删除日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date deleteCaseDateStart;

    @ApiModelProperty("删除日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date deleteCaseDateEnd;

    @ApiModelProperty("委案金额开始")
    private Double overdueAmtTotalStart;

    @ApiModelProperty("委案金额结束")
    private Double overdueAmtTotalEnd;

    @ApiModelProperty(notes = "欠款金额开始")
    private Double leftAmtStart;

    @ApiModelProperty(notes = "欠款金额结束")
    private Double leftAmtEnd;

    @ApiModelProperty(notes = "退案天数")
    private Integer retireCaseDays;

    @ApiModelProperty(notes = "案件类型")
    private CaseType caseType;

    public BoolQueryBuilder generateQueryBuilder() {
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        if(Objects.nonNull(principalId) && !principalId.isEmpty()){
            qb.must(termsQuery("principalId.keyword",principalId));
        }
        if (Objects.nonNull(this.batchNumber)) {
            qb.must(matchPhraseQuery("batchNumber", this.batchNumber));
        }
        if (Objects.nonNull(this.personalName)) {
            qb.must(matchPhraseQuery("personalName", this.personalName));
        }
        if (Objects.nonNull(this.certificateNo)) {
            qb.must(matchPhraseQuery("certificateNo", this.certificateNo));
        }
        if (Objects.nonNull(this.cardNo)) {
            qb.must(wildcardQuery("cardNo", StringUtils.join("*", this.cardNo, "*")));
        }
        if (Objects.nonNull(this.phone)) {
            qb.must(wildcardQuery("phone.keyword", StringUtils.join( this.phone, "*")));
        }
        if (Objects.nonNull(this.stage)) {
            qb.must(matchPhraseQuery("stage", this.stage));
        }
        if (Objects.nonNull(this.retireCaseDays)) {
            qb.must(matchPhraseQuery("retireCaseDays", this.retireCaseDays));
        }
        if (Objects.nonNull(this.caseNumber)) {
            qb.must(matchPhraseQuery("caseNumber.keyword", this.caseNumber));
        }
        if (Objects.nonNull(this.employerName)) {
            qb.must(wildcardQuery("employerName.keyword", StringUtils.join("*", this.employerName, "*")));
        }
        if (StringUtils.isNotBlank(this.address)) {
            qb.must(QueryBuilders.boolQuery().should(QueryBuilders.wildcardQuery("homeAddr.keyword", "*" + org.apache.commons.lang.StringUtils.trim(this.address) + "*"))
                    .should(QueryBuilders.wildcardQuery("residenceAddr.keyword", "*" + org.apache.commons.lang.StringUtils.trim(this.address) + "*"))
                    .should(QueryBuilders.wildcardQuery("employerAddr.keyword", "*" + org.apache.commons.lang.StringUtils.trim(this.address) + "*"))
                    .should(QueryBuilders.wildcardQuery("billAddr.keyword", "*" + org.apache.commons.lang.StringUtils.trim(this.address) + "*")));
        }
        if (Objects.nonNull(this.overdueAmtTotalStart)) {
            qb.must(rangeQuery("overdueAmtTotal").gte(this.overdueAmtTotalStart));
        }
        if (Objects.nonNull(this.overdueAmtTotalEnd)) {
            qb.must(rangeQuery("overdueAmtTotal").lte(this.overdueAmtTotalEnd));
        }
        if (Objects.nonNull(this.leftAmtStart)) {
            qb.must(rangeQuery("leftAmt").gte(this.leftAmtStart));
        }
        if (Objects.nonNull(this.leftAmtEnd)) {
            qb.must(rangeQuery("leftAmt").lte(this.leftAmtEnd));
        }
        if (Objects.nonNull(this.delegationDateEnd)) {
            qb.must(rangeQuery("delegationDate").lte(this.delegationDateEnd.getTime()));
        }
        if (Objects.nonNull(this.delegationDateStart)) {
            qb.must(rangeQuery("delegationDate").gte(this.delegationDateStart.getTime()));
        }
        if (Objects.nonNull(this.overduePeriodsStart)) {
            qb.must(rangeQuery("overduePeriods").gte(this.overduePeriodsStart));
        }
        if (Objects.nonNull(this.overduePeriodsEnd)) {
            qb.must(rangeQuery("overduePeriods").lte(this.overduePeriodsEnd));
        }
        if (Objects.nonNull(this.overdueDaysStart)) {
            qb.must(rangeQuery("overdueDays").gte(this.overdueDaysStart));
        }
        if (Objects.nonNull(this.overdueDaysEnd)) {
            qb.must(rangeQuery("overdueDays").lte(this.overdueDaysEnd));
        }
        if (Objects.nonNull(this.city)) {
            qb.must(matchPhraseQuery("city", this.city));
        }
        if (Objects.nonNull(this.endCaseDateStart)) {
            qb.must(rangeQuery("endCaseDate").gte(this.endCaseDateStart.getTime()));
        }
        if (Objects.nonNull(this.endCaseDateEnd)) {
            qb.must(rangeQuery("endCaseDate").lte(this.endCaseDateEnd.getTime() + 86400000));
        }
        if (Objects.nonNull(this.deleteCaseDateStart)) {
            qb.must(rangeQuery("deleteCaseDate").gte(this.deleteCaseDateStart.getTime()));
        }
        if (Objects.nonNull(this.deleteCaseDateEnd)) {
            qb.must(rangeQuery("deleteCaseDate").lte(this.deleteCaseDateEnd.getTime() + 86400000));
        }
        if (Objects.nonNull(this.payStatus)) {
            qb.must(matchPhraseQuery("payStatus", this.payStatus.name()));
        }
        if (Objects.nonNull(this.caseType)) {
            qb.must(matchPhraseQuery("caseType", this.caseType.name()));
        }
        if (Objects.nonNull(this.principalId)) {
            qb.must(matchPhraseQuery("principalId.keyword", this.principalId));
        }
        return qb;
    }
}
